package com.zhdl.hardware_contact.hardware_web.service;

import com.zhdl.hardware_contact.hardware_web.request.ControlCmdSetReq;
import com.zhdl.hardware_contact.hardware_web.request.ControlCmdSetValueReq;
import com.zhdl.hardware_contact.hardware_web.respone.HardwareInfoViewResp;
import com.zhdl.hardware_contact.hardware_web.respone.ProjectActionConfigResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service("ProcessSportService")
public class ProcessSportService {

    @Resource
    private HardwareContactService hardwareContactService;
    @Resource
    private HardwareAbsService hardwareAbsService;

    /**
     * 电机Abs运动
     *
     * @param Name 硬件名称
     * @param location 第几个位置
     */
    public void HardwareAbsRun(String Name,Integer location){
        HardwareInfoViewResp hardwareInfo = hardwareContactService.getHardwareInfoByName(Name);
        log.error("{}",hardwareInfo.getAbsPosList());
        Integer absId = hardwareInfo.getAbsPosList().get(location).getId();
        hardwareAbsService.controlHardwareRun(absId);
    }


    /**
     * 电机Relative运动
     *
     * @param Name 硬件名称
     * @param relativePos 位置
     * @param relativeVel 速度
     */
    public void HardwareRelativeRun(String Name,String relativePos,String relativeVel){
        HardwareInfoViewResp hardwareInfo = hardwareContactService.getHardwareInfoByName(Name);
        ControlCmdSetReq controlCmdSetReq = new ControlCmdSetReq();
        for (ProjectActionConfigResp projectActionConfigResp : hardwareInfo.getConfigActionList()) {
            if(projectActionConfigResp.getName().equals("relative")){
                controlCmdSetReq.setHardwareId(projectActionConfigResp.getHardwareId());
                controlCmdSetReq.setActionId(projectActionConfigResp.getId());

                List<ControlCmdSetValueReq> actionParam = new ArrayList<>();

                //固定赋值第一位为relativePos,第二位为relativeVel,第三位为relativeTrig
                //relativePos
                ControlCmdSetValueReq controlCmdSetValueReq = new ControlCmdSetValueReq();
                controlCmdSetValueReq.setParamId(projectActionConfigResp.getHardwareInfoActionConfigRespList().get(0).getId());
                controlCmdSetValueReq.setParamValue(relativePos);
                actionParam.add(controlCmdSetValueReq);

                //relativeVel
                ControlCmdSetValueReq controlCmdSetValueReq1 = new ControlCmdSetValueReq();
                controlCmdSetValueReq1.setParamId(projectActionConfigResp.getHardwareInfoActionConfigRespList().get(1).getId());
                controlCmdSetValueReq1.setParamValue(relativeVel);
                actionParam.add(controlCmdSetValueReq1);

                //relativeTrig
                ControlCmdSetValueReq controlCmdSetValueReq2 = new ControlCmdSetValueReq();
                controlCmdSetValueReq2.setParamId(projectActionConfigResp.getHardwareInfoActionConfigRespList().get(2).getId());
                controlCmdSetValueReq2.setParamValue("1");
                actionParam.add(controlCmdSetValueReq2);


                controlCmdSetReq.setActionParam(actionParam);
                hardwareContactService.controlHardwareRun(controlCmdSetReq);
            }
        }
    }


    /**
     * 气缸开关
     *
     * @param Name 硬件名称
     * @param switches 开-true 关-false
     */
    public void HardwareCylinderSwitches(String Name,Boolean switches){
        HardwareInfoViewResp hardwareInfo = hardwareContactService.getHardwareInfoByName(Name);
        ControlCmdSetReq controlCmdSetReq = new ControlCmdSetReq();
        String open = "0";
        String close = "1";
        if(switches){
            open = "1";
            close = "0";
        }
        for (ProjectActionConfigResp projectActionConfigResp : hardwareInfo.getConfigActionList()) {
                if(projectActionConfigResp.getName().equals("open")){
                    controlCmdSetReq.setHardwareId(projectActionConfigResp.getHardwareId());
                    controlCmdSetReq.setActionId(projectActionConfigResp.getId());

                    List<ControlCmdSetValueReq> actionParam = new ArrayList<>();

                    //固定赋值第一位为open,第二位为close
                    //open
                    ControlCmdSetValueReq controlCmdSetValueReq = new ControlCmdSetValueReq();
                    controlCmdSetValueReq.setParamId(projectActionConfigResp.getHardwareInfoActionConfigRespList().get(0).getId());
                    controlCmdSetValueReq.setParamValue(open);
                    actionParam.add(controlCmdSetValueReq);

//                //close
//                ControlCmdSetValueReq controlCmdSetValueReq1 = new ControlCmdSetValueReq();
//                controlCmdSetValueReq1.setParamId(projectActionConfigResp.getHardwareInfoActionConfigRespList().get(1).getId());
//                controlCmdSetValueReq1.setParamValue(close);
//                actionParam.add(controlCmdSetValueReq1);


                    controlCmdSetReq.setActionParam(actionParam);
                    hardwareContactService.controlHardwareRun(controlCmdSetReq);
                }


            if(projectActionConfigResp.getName().equals("close")){
                controlCmdSetReq.setHardwareId(projectActionConfigResp.getHardwareId());
                controlCmdSetReq.setActionId(projectActionConfigResp.getId());

                List<ControlCmdSetValueReq> actionParam = new ArrayList<>();

                //固定赋值第一位为open,第二位为close
//                //open
//                ControlCmdSetValueReq controlCmdSetValueReq = new ControlCmdSetValueReq();
//                controlCmdSetValueReq.setParamId(projectActionConfigResp.getHardwareInfoActionConfigRespList().get(0).getId());
//                controlCmdSetValueReq.setParamValue(open);
//                actionParam.add(controlCmdSetValueReq);

                //close
                ControlCmdSetValueReq controlCmdSetValueReq1 = new ControlCmdSetValueReq();
                controlCmdSetValueReq1.setParamId(projectActionConfigResp.getHardwareInfoActionConfigRespList().get(0).getId());
                controlCmdSetValueReq1.setParamValue(close);
                actionParam.add(controlCmdSetValueReq1);


                controlCmdSetReq.setActionParam(actionParam);
                hardwareContactService.controlHardwareRun(controlCmdSetReq);
            }
        }
    }



}
